package cn.kgc.blogservice.service;

import cn.kgc.blogcommon.pojo.Category;
import cn.kgc.blogcommon.pojo.News;
import cn.kgc.blogcommon.pojo.Type;
import cn.kgc.blogcommon.pojo.User;
import cn.kgc.blogservice.mapper.BlogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class BlogServiceImpl implements BlogService{

    //跨服务器访问
    private RestTemplate restTemplate;

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public List<Type> getAllType() {
        return blogMapper.getAllType();
    }

    @Override
    public List<Category> getCategory() {
        return blogMapper.getCategory();
    }

    @Override
    public List<News> getAllNews(Integer categoryId) {
        return blogMapper.getAllNews(categoryId);
    }

    @Override
    public User getUserById(Integer userId) {
        String url = String.format("%s?userId=%d", "http://localhost:8082/getUserById/", userId);
        return restTemplate.getForObject(url, User.class);
    }

    @Override
    public News getNewsById(Integer newsId) {
        return blogMapper.getNewsById(newsId);
    }

    @Override
    public List<Category> getPopularCategory() {
        return blogMapper.getPopularCategory();
    }

    @Override
    public List<Category> getCategoryByTypeId(Integer typeId) {
        return blogMapper.getCategoryByTypeId(typeId);
    }

    @Override
    public List<Integer> getMinId() {
        return blogMapper.getMinId();
    }

    @Override
    public List<News> getHotNews(List<Integer> newsIdList) {
        List<News> list = blogMapper.getHotNews(newsIdList);
        for(News n : list){
            System.out.println(n.getTagList().toString());
        }
        return blogMapper.getHotNews(newsIdList);
    }
}
